import Basemap from '@/assets/api/basemap'
import AMap from 'AMap';
import AMapUI from 'AMapUI';
import centerstarPng from '@/assets/mapimg/centerstar.png'
import centerstarTwoPng from '@/assets/mapimg/centerStarTwo.png'
import nearCityPng from '@/assets/mapimg/nearCity.png'
import nearCityPngThree from '@/assets/mapimg/nearCity3.png'
import checkedPng from '@/assets/mapimg/checked.png'
import nocheckedPng from '@/assets/mapimg/nochecked.png'
function GaoDemap(id, eqCenter) {
    this.eqCenter = eqCenter;
    this.mapData = null;
    this.bDistanceLine = [];
    this.amap = new AMap.Map(id);
    //var centerPoint = new AMap.LngLat(this.eqCenter.lon,this.eqCenter.lat)
    var centerPoint = new AMap.LngLat(this.eqCenter.lon, this.eqCenter.lat)
    this.amap.setZoomAndCenter(8, centerPoint)
    //实时交通
    this.trafficLayer = new AMap.TileLayer.Traffic({
        zIndex: 10
    });
    this.trafficLayer.setMap(this.amap);
    this.RoadNet = new AMap.TileLayer.RoadNet({
        zIndex:9
    })
    this.RoadNet.setMap(this.amap)
    this.Satellite  =new AMap.TileLayer.Satellite({
        zIndex:9
    })
    this.Satellite.setMap(this.amap)
    this.Satellite.hide()
    //var navOpt = {type: BMAP_NAVIGATION_CONTROL_ZOOM,offset:new AMap.Size(10, 260)};        
    //this.amap.addControl(new BMap.NavigationControl(navOpt)); 
    AMapUI.loadUI(['control/BasicControl'], function (BasicControl) {

        //添加一个缩放控件
        this.amap.addControl(new BasicControl.Zoom({
            position: 'rm'
        }));

    }.bind(this));

}
GaoDemap.prototype = new Basemap();  //继承Basemap
GaoDemap.prototype.setMarker = function (data, type) {
    let map = this.amap;
    if (type == "nearCity") {
        var points = [];
        data.forEach(function (item, index) {
            if (item.longtitude && item.latitude) {
                if (index != 0) {
                    //第一个点为震中，已添加
                    var point = new AMap.LngLat(item.longtitude, item.latitude);
                    var icon = new AMap.Icon({ image: nearCityPng, size: new AMap.Size(20, 20)});
                    var marker = new AMap.Marker({ position: point, icon: icon ,offset:new AMap.Pixel(-9,-20) });
                    var content = this.setContent('附近城镇信息', item, "nearCity");
                    marker.on('click', function () {
                        var InfoWindowOptions = {
                            content: content,
                            isCustom: false,
                            autoMove:true,
                            offset:new AMap.Pixel(0,-11)
                        };
                        var infoWindow = new AMap.InfoWindow(InfoWindowOptions);  // 创建信息窗口对象 
                        infoWindow.open(map, point);
                    })
                    marker.setMap(map);
                    points.push(point);
                    //添加折线
                    var PolylineOptions = {
                        path: [new AMap.LngLat(this.eqCenter.lon, this.eqCenter.lat), new AMap.LngLat(item.longtitude, item.latitude)],
                        strokeColor: "blue",
                        strokeOpacity: 0.5,
                        strokeWeight: 2
                    }
                    var polyline = new AMap.Polyline(PolylineOptions);
                    this.bDistanceLine.push(polyline)
                    polyline.setMap(map);
                    //map.addOverlay(polyline); 
                    //高德没有这个功能 
                    //很无奈
                    //polyline.disableMassClear()
                    var opts = {
                        text: item.distence.toFixed(0) + "公里",
                        position: point,    // 指定文本标注所在的地理位置
                        offset: new AMap.Pixel(0, -45)    //设置文本偏移量
                    }
                    var label = new AMap.Text( opts);  // 创建文本标注对象
                    label.setStyle({
                        color: "red",
                        fontSize: "12px",
                        height: "20px",
                        lineHeight: "20px",
                        fontFamily: "微软雅黑"
                    });
                    label.setMap(map);
                    //map.addOverlay(label);
                }
                index++;
            }
        }.bind(this));
        this.bmapMarkers = points
        //this.bmap.setViewport(points);
    } else if (type == "EqCenter") {
        var centerPoint = new AMap.LngLat(data.lon, data.lat)
        var icon = new AMap.Icon({image:centerstarPng,size: new AMap.Size(23, 23)})
        var centerMarker = new AMap.Marker({position:centerPoint,  icon: icon ,offset:new AMap.Pixel(-10,-10)})
        centerMarker.setMap(map);
        //map.addOverlay(centerMarker)
        //this.bmap.centerAndZoom(centerPoint, 10)
        map.setZoomAndCenter(10, centerPoint)
        var content = this.setContent('震中信息', this.eqCenter, "EqCenter");
        centerMarker.on('click',function () {
            //setInfoWindow(map, content, point);
            var InfoWindowOptions = {
                content: content,
                //size: { width: 0, height: 0 },
                isCustom: false,
                autoMove:true,
                offset:new AMap.Pixel(0,0),
            };
            var infoWindow = new AMap.InfoWindow(InfoWindowOptions);  // 创建信息窗口对象 
            //map.openInfoWindow(infoWindow,point); //开启信息窗口
            infoWindow.open(map, centerPoint);
            //var infoWindow = new AMap.InfoWindow(content, { width: 0, height: 0 });  // 创建信息窗口对象 
            //map.openInfoWindow(infoWindow, centerPoint); //开启信息窗口
        })


        // centerMarker.addEventListener('click', function () {
        //     //setInfoWindow(map, content, point);
        //     var infoWindow = new BMap.InfoWindow(content, { width: 0, height: 0 });  // 创建信息窗口对象 
        //     map.openInfoWindow(infoWindow, centerPoint); //开启信息窗口
        // })
        //震中闪烁
        //var icon = new AMap.Icon({image:centerstarPng,size: new AMap.Size(23, 23)})
        
        var iconTwo = new AMap.Icon({image:centerstarTwoPng,size: new AMap.Size(20, 20)});
        setInterval(function () {
            if (centerMarker.getIcon() == icon) {
                centerMarker.setIcon(iconTwo)
            } else {
                centerMarker.setIcon(icon)
            }
        }, 1000)
    }
};
GaoDemap.prototype.setCounty = function (data) {
    let map = this.amap;
    AMap.service('AMap.DistrictSearch', function() {
        var opts = {
            subdistrict: 1,   //返回下一级行政区
            extensions: 'all',  //返回行政区边界坐标组等具体信息
            level: 'city'  //查询行政级别为 市
        };
        //实例化DistrictSearch
        var district = new AMap.DistrictSearch(opts);
        district.setLevel('district');
        //行政区查询

        data.forEach(function (item, index) {
            district.search(item.name, function(status, result) {
                if(status=='no_data'){
                    return ;
                }
                var bounds = result.districtList[0].boundaries;
                //var polygons = [];
                if (bounds) {
                    for (var i = 0, l = bounds.length; i < l; i++) {
                        //生成行政区划polygon
                        var polygon = new AMap.Polyline({
                            map: map,
                            strokeWeight: 1,
                            path: bounds[i],
                            fillOpacity: 0,
                            fillColor: 'transparent',
                            strokeColor: '#000',
                            strokeStyle:'dashed'
                        });
                        //polygons.push(polygon);
                    }
                    //map.setFitView();//地图自适应
                }
            });
        })
    })

    // var option = {
    //     strokeWeight: 1,
    //     strokeColor: "#000",
    //     StrokeStyle: "dashed", //设置多边形边线样式为实线或虚线，取值 solid 或 dashed
    //     fillColor: 'transparent', //多边形填充色
    //     fillOpacity: 0 //设置多边形填充颜色透明度0-1
    // }
    // var bdary = new BMap.Boundary();
    // data.forEach(function (item, index) {
    //     bdary.get(item.name, function (rs) {
    //         var count = rs.boundaries.length
    //         for (var i = 0; i < count; i++) {
    //             var ply = new BMap.Polygon(rs.boundaries[i], option); //建立多边形覆盖物
    //             map.addOverlay(ply);  //添加覆盖物
    //         }
    //     })
    // })
};
// GaoDemap.prototype.myControlInit = function () {
//     // var top_left_control = new BMap.ScaleControl({ anchor: BMAP_ANCHOR_BOTTOM_LEFT });// 左上角，添加比例尺
//     // top_left_control.setOffset(new AMap.Size(1, 2))
//     // this.bmap.addControl(top_left_control);
//     AMapUI.loadUI(['control/BasicControl'], function (BasicControl) {
//         //添加一个缩放控件
//         amap.addControl(new BasicControl.Zoom({
//             position: 'rm'
//         }));



//     });
//     var trafficLayer = new AMap.TileLayer.Traffic({
//         zIndex: 10
//     });
//     trafficLayer.setMap(map);
//     //var navOpt = { type: BMAP_NAVIGATION_CONTROL_ZOOM, offset: new AMap.Size(10, 260) };
//     //this.bmap.addControl(new BMap.NavigationControl(navOpt));
//     //var trafficLayer = new BMap.TrafficLayer();
//     //this.bmap.addTileLayer(trafficLayer);
//     function myControl(amap, Offset, className, innerHTML, onclick) {
//         // 默认停靠位置和偏移量
//         this.defaultAnchor = BMAP_ANCHOR_TOP_LEFT;
//         this.defaultOffset = Offset;
//         this.initialize = function (amap) {
//             // 创建一个DOM元素
//             var div = document.createElement("div");
//             div.className = className;
//             div.innerHTML = innerHTML;
//             div.onclick = onclick;
//             // 添加DOM元素到地图中
//             amap.getContainer().appendChild(div);
//             // 将DOM元素返回
//             return div;
//         }
//     }
//     myControl.prototype = new BMap.Control();
//     var bmap = this.bmap;
//     var myTraffic = new myControl(this.bmap, new AMap.Size(10, 105), 'track', '<span class="track-icon" data="checked"><img src=' + checkedPng + ' style="width:1.5rem;height:1.5rem"></span><span class="track-title">路网</span>', function () {
//         var _this = this;
//         var parentNode = _this.childNodes[0];
//         var node = parentNode.childNodes[0];
//         var currentData = _this.childNodes[0].attributes[1].nodeValue;
//         if (currentData === 'nochecked') {
//             node.setAttribute('src', checkedPng);
//             parentNode.setAttribute('data', 'checked');
//             bmap.setMapStyle({
//                 styleJson: [{
//                     "featureType": "road",
//                     "elementType": "all",
//                     "stylers": {
//                         "visibility": "on"
//                     }
//                 }]
//             });
//         } else {
//             node.setAttribute('src', nocheckedPng);
//             parentNode.setAttribute('data', 'nochecked');
//             bmap.setMapStyle({
//                 styleJson: [{
//                     "featureType": "road",
//                     "elementType": "all",
//                     "stylers": {
//                         "visibility": "off"
//                     }
//                 }]
//             });
//         }
//     });
//     var realTimeTraffic = new myControl(this.bmap, new AMap.Size(10, 130), 'track', '<span class="track-icon" data="checked"><img src=' + checkedPng + ' style="width:1.5rem;height:1.5rem"></span><span class="track-title">实时路况</span>', function () {
//         var _this = this;
//         var parentNode = _this.childNodes[0];
//         var node = parentNode.childNodes[0];
//         var currentData = _this.childNodes[0].attributes[1].nodeValue;
//         if (currentData === 'nochecked') {
//             node.setAttribute('src', checkedPng);
//             parentNode.setAttribute('data', 'checked');
//             trafficLayer = new BMap.TrafficLayer();
//             bmap.addTileLayer(trafficLayer);
//         } else {
//             node.setAttribute('src', nocheckedPng);
//             parentNode.setAttribute('data', 'nochecked');
//             bmap.removeTileLayer(trafficLayer);
//             trafficLayer = null;
//         }
//     })
//     // this.bmap.addControl(myToEqCenter);
//     // this.bmap.addControl(myCompass);
//     this.bmap.addControl(myTraffic);
//     this.bmap.addControl(realTimeTraffic);
// }
GaoDemap.prototype.setFaultZone = function (data) {
    let map = this.bmap;
    var point = new AMap.LngLat(this.eqCenter.lon, this.eqCenter.lat); // 创建点坐标
    var centerIcon = new AMap.Icon({image:centerstarPng,size: new AMap.Size(20, 20)});
    var centerMarker = new AMap.Marker({position:point, icon: centerIcon });
    marker.setMap(map);
    //map.addOverlay(centerMarker);
    var opts = {
        text:this.eqCenter.district + this.eqCenter.magn + "级地震",
        position: point,    // 指定文本标注所在的地理位置
        offset: new AMap.Pixel(-80, -30)    //设置文本偏移量
    }
    var label = new AMap.Text(opts);  // 
    label.setStyle({
        color: "black",
        fontSize: "18px",
        height: "20px",
        lineHeight: "20px",
        fontFamily: "斜体",
        borderColor: 'transparent',
        backgroundColor: 'transparent'
    });
    label.setMap(map);
    //map.addOverlay(label);
    var linePoints = new Array();
    var sub = 0;
    linePoints[sub] = new Array();
    var info = new Array();
    info[sub] = new Array();
    let lonMin = Number(this.eqCenter.lon) - 3;
    let lonMax = Number(this.eqCenter.lon) + 3;
    let latMin = Number(this.eqCenter.lat) - 3;
    let latMax = Number(this.eqCenter.lat) + 3;
    for (var i = 0; i + 1 < data.RECORDS.length; i++) {
        if (data.RECORDS[i].line == data.RECORDS[i + 1].line) {
            var point = new AMap.LngLat(data.RECORDS[i].baidu_X, data.RECORDS[i].baidu_Y);
            linePoints[sub].push(point);
        } else {
            var point = new AMap.LngLat(data.RECORDS[i].baidu_X, data.RECORDS[i].baidu_Y);
            linePoints[sub].push(point);
            // if(data.RECORDS[i].name!=""){
            // 	info[sub].push(data.RECORDS[i].name);
            // }else{
            // 	info[sub].push("");
            // }
            sub++;
            linePoints[sub] = new Array();
        }
    }
    for (var j = 0; j < linePoints.length; j++) {

        if (linePoints[j][0].lng < lonMax && linePoints[j][0].lng > lonMin && linePoints[j][0].lat > latMin && linePoints[j][0].lat < latMax) {
            var polyline = new AMap.Polyline(linePoints[j], { strokeColor: "red", strokeWeight: 2, strokeOpacity: 0.5 });
            var opts = {
                position: point,    // 指定文本标注所在的地理位置
                offset: new AMap.Pixel(30, -30)    //设置文本偏移量
            }
            var label = new AMap.Text("", opts);  // 创建文本标注对象
            label.setStyle({
                color: "red",
                fontSize: "12px",
                height: "20px",
                lineHeight: "20px",
                fontFamily: "微软雅黑",
                borderColor: 'transparent',
                backgroundColor: 'transparent'
            });

            //？？？
            polyline.flag = "myfault";
            polyline.setMap(map);
            //map.addOverlay(polyline);
            //map.addOverlay(label);
        }


    }
};
GaoDemap.prototype.setCenterView = function () {
    var point = new AMap.LngLat(this.eqCenter.lon, this.eqCenter.lat);
    this.bmap.setZoomAndCenter(7.5,point);
};
GaoDemap.prototype.showDistanceLine = function (flag) {
    if (flag == true) {
        for (var i = 0; i < this.bDistanceLine.length; i++) {
            this.bDistanceLine[i].show();
        }
    } else if (flag == false) {
        for (var i = 0; i < this.bDistanceLine.length; i++) {
            this.bDistanceLine[i].hide();
        }
    }

}
export default GaoDemap;